import { defineStore } from 'pinia'
import http from '@/utils/uc-http'
import logo from '@/assets/logo/logo.png'
import logo66 from '@/assets/logo/logo66.png'
import loginlogo from '@/assets/login/logo.png'
import loginlogo66 from '@/assets/login/logo66.png'

const TOKEN_KEY = 'token'

export const useUserStore = defineStore('user', () => {
  const user = ref(JSON.parse(localStorage.user || '{}'))
  const token = ref(localStorage[TOKEN_KEY])
  const route = useRoute()
  const router = useRouter()
  const showerrordialog = ref(false)
  const userInfo = ref(null)
  const userdata = ref({})
  const packageJs = ref({})
  const logostyletype = ref(1)
  let checkinfoIntervalId

  // 判断是否登录
  const checkLogin = (isGoLogin = true) => {
    token.value = localStorage[TOKEN_KEY]


    if (token.value) {

      // if (packageJs.value.plantcode == undefined) {
      //   setTimeout(() => {
      //     checkInfo()
      //   }, 500);
      // }
      return true
    } else {
      if (isGoLogin) logout(true)
      return false
    }
    return true
  }

  // 跳转登录页面
  function goLogin(isRedirect = true) {
    const query = {}
    // 登录成功自动返回
    if (isRedirect) query.redirect = encodeURIComponent(location.pathname + location.search) // NOTE 这里用的location，route.path可以被拦截跳转
    router.replace({
      path: '/login',
      query,
    })
  }

  // 登录成功
  const loginSuccess = async options => {
    user.value = options
    userdata.value = options
    token.value = options.accessToken

    localStorage.user = JSON.stringify(options)
    checkInfo()

    localStorage.ql_admin_gid = options.gid
    localStorage.ql_admin_gname = options.gname
    localStorage.ql_admin_roles = options.username
    localStorage[TOKEN_KEY] = token.value
    localStorage.ql_admin_uid = options.uid

    router.replace({
      path: '/',
    })
  }

  // 获取用户信息
  const getUserInfo = () => {
    return Promise.resolve()
    // return getUserInfoApi().then(res => {
    //   userInfo.value = res.data
    //   return res.data
    // })
  }

  // 退出
  const logout = isRedirect => {
    // localStorage.clear()
    localStorage.removeItem(TOKEN_KEY)
    localStorage.removeItem('user')

    user.value = null
    userInfo.value = null
    token.value = null
    clearInterval(checkinfoIntervalId)
    goLogin(isRedirect)
  }

  // 判断账号是否有效
  const checkInfo = async () => {

    if (!token.value) return
    let mac = await window.electron.getMacAddress();
    clearInterval(checkinfoIntervalId)
    if (packageJs.value.plantcode) {
      http.post('/user_check', {
        token: token.value,
        plant_code: packageJs.value.plantcode,
        mac: mac,
      }).then((res) => {
        userdata.value = res

        checkinfoIntervalId = setInterval(() => {
          checkInfo()
        }, 30000)
      })
    } else {
      setTimeout(() => {
        checkInfo()
      }, 1000);
    }
  }

  const openerrordialog = () => {
    showerrordialog.value = true
  }

  const savepackageJs = (packagejs) => {
    packageJs.value = packagejs
  }

  const logoimg = () => {
    let productname = packageJs.value.productName
    let logoimg
    switch (productname) {
      case '风车体育':
        logoimg = logo
        logostyletype.value = 1
        break
      case '77代理':
        logoimg = logo66
        logostyletype.value = 2
        break
      default:
        break
    }
    return logoimg
  }

  const loginlogoimg = () => {
    let productname = packageJs.value.productName
    let logoimg
    switch (productname) {
      case '风车体育':
        logoimg = loginlogo
        logostyletype.value = 1
        break
      case '77代理':
        logoimg = loginlogo66
        logostyletype.value = 2
        break
      default:
        break
    }
    return logoimg
  }

  checkInfo()

  return {
    user,
    userInfo,
    token,
    showerrordialog,
    packageJs,
    logostyletype,
    userdata,

    checkLogin,
    loginSuccess,
    goLogin,
    getUserInfo,
    logout,
    checkInfo,
    openerrordialog,
    savepackageJs,
    logoimg,
    loginlogoimg
  }
})
